home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Entertainment
/
tblt
/
tblt⁄grid.c
< prev
next >
Wrap
Text File
|
1986-09-06
|
6KB
|
175 lines
/*
* grid.c - support for the grid abstraction of the board
*/
#include <quickdraw.h>
#include <window.h>
#include <memory.h>
#include "tablut.h"
/*
* setpieces() - put all the board pieces in their initial places
*/
setpieces()
{
int i;
for (i = 0; i < NUMPIECES; ++i) {
hidepiece(i);
}
placepiece(FIRSTSWEDE , 0, -2); placepiece(FIRSTSWEDE+1, 0, -1);
placepiece(FIRSTSWEDE+2, -2, 0); placepiece(FIRSTSWEDE+3, -1, 0);
placepiece(FIRSTSWEDE+4, 1, 0); placepiece(FIRSTSWEDE+5, 2, 0);
placepiece(FIRSTSWEDE+6, 0, 1); placepiece(FIRSTSWEDE+7, 0, 2);
placepiece(FIRSTMUSC , -1, -4); placepiece(FIRSTMUSC+ 1, 0, -4);
placepiece(FIRSTMUSC+ 2, 1, -4); placepiece(FIRSTMUSC+ 3, 0, -3);
placepiece(FIRSTMUSC+ 4, 4, -1); placepiece(FIRSTMUSC+ 5, 4, 0);
placepiece(FIRSTMUSC+ 6, 4, 1); placepiece(FIRSTMUSC+ 7, 3, 0);
placepiece(FIRSTMUSC+ 8, 1, 4); placepiece(FIRSTMUSC+ 9, 0, 4);
placepiece(FIRSTMUSC+10, -1, 4); placepiece(FIRSTMUSC+11, 0, 3);
placepiece(FIRSTMUSC+12, -4, 1); placepiece(FIRSTMUSC+13, -4, 0);
placepiece(FIRSTMUSC+14, -4, -1); placepiece(FIRSTMUSC+15, -3, 0);
placepiece(THEKING, 0, 0);
}
/*
* makehomes() - initialize the off-board home for each piece.
* This code assumes that *home[].tenant is initialized to 0.
*/
makehomes()
{
SetPt(&(kinghome[0].hpoint), -6, 0); gridtopoint(&(kinghome[0].hpoint));
SetPt(&(swedhome[0].hpoint), -6, 1); gridtopoint(&(swedhome[0].hpoint));
SetPt(&(swedhome[1].hpoint), -6, 2); gridtopoint(&(swedhome[1].hpoint));
SetPt(&(swedhome[2].hpoint), -6, 3); gridtopoint(&(swedhome[2].hpoint));
SetPt(&(swedhome[3].hpoint), -6, 4); gridtopoint(&(swedhome[3].hpoint));
SetPt(&(swedhome[4].hpoint), -7, 1); gridtopoint(&(swedhome[4].hpoint));
SetPt(&(swedhome[5].hpoint), -7, 2); gridtopoint(&(swedhome[5].hpoint));
SetPt(&(swedhome[6].hpoint), -7, 3); gridtopoint(&(swedhome[6].hpoint));
SetPt(&(swedhome[7].hpoint), -7, 4); gridtopoint(&(swedhome[7].hpoint));
SetPt(&(muschome[0].hpoint), 6, -3); gridtopoint(&(muschome[0].hpoint));
SetPt(&(muschome[1].hpoint), 6, -2); gridtopoint(&(muschome[1].hpoint));
SetPt(&(muschome[2].hpoint), 6, -1); gridtopoint(&(muschome[2].hpoint));
SetPt(&(muschome[3].hpoint), 6, 0); gridtopoint(&(muschome[3].hpoint));
SetPt(&(muschome[4].hpoint), 6, 1); gridtopoint(&(muschome[4].hpoint));
SetPt(&(muschome[5].hpoint), 6, 2); gridtopoint(&(muschome[5].hpoint));
SetPt(&(muschome[6].hpoint), 6, 3); gridtopoint(&(muschome[6].hpoint));
SetPt(&(muschome[7].hpoint), 6, 4); gridtopoint(&(muschome[7].hpoint));
SetPt(&(muschome[8].hpoint), 7, -3); gridtopoint(&(muschome[8].hpoint));
SetPt(&(muschome[9].hpoint), 7, -2); gridtopoint(&(muschome[9].hpoint));
SetPt(&(muschome[10].hpoint), 7, -1); gridtopoint(&(muschome[10].hpoint));
SetPt(&(muschome[11].hpoint), 7, 0); gridtopoint(&(muschome[11].hpoint));
SetPt(&(muschome[12].hpoint), 7, 1); gridtopoint(&(muschome[12].hpoint));
SetPt(&(muschome[13].hpoint), 7, 2); gridtopoint(&(muschome[13].hpoint));
SetPt(&(muschome[14].hpoint), 7, 3); gridtopoint(&(muschome[14].hpoint));
SetPt(&(muschome[15].hpoint), 7, 4); gridtopoint(&(muschome[15].hpoint));
}
/*
* makegrid() - initialize the record of what piece is on what square.
*/
makegrid()
{
static struct pieceimage *realgrid[2+9+2][2+9+2]; /* the actual storage */
short h, v;
gridp = &realgrid[2+4][2+4]; /* center the grid on [0][0] */
for (h = -(2+4); h <= 4+2; ++h) {
for (v = -(2+4); v <= 4+2; ++v) {
(*gridp)[h][v] = (struct pieceimage *) 0;
}
}
}
/*
* placepiece() - move the given piece to the given grid position
*/
placepiece(pidx, gh, gv)
int pidx; /* index of the piece to move */
short gh, gv; /* grid coordinates to move the piece to */
{
struct pieceimage *p;
Point destpoint; /* window coordinates to move to */
p = &piece[pidx];
if (onscreen(p) && !onboard(p)) {
p->curhome->tenant = (struct pieceimage *) 0;
p->curhome = &weirdhome;
}
if (onboard(p)) {
(*gridp)[p->grid.h][p->grid.v] = (struct pieceimage *) 0;
}
p->grid.h = gh; p->grid.v = gv;
(*gridp)[p->grid.h][p->grid.v] = p;
destpoint.h = gh; destpoint.v = gv;
gridtopoint(&destpoint);
sweeppiece(pidx, &destpoint);
}
/*
* removepiece() - remove the given piece from the board,
* placing it in the appropriate off-board position.
*/
removepiece(pidx)
int pidx; /* index of the piece to remove */
{
struct pieceimage *p;
int wasonboard; /* true if the piece was originally on-board */
struct piecehome *lasthp; /* a temp home-pointer */
struct piecehome *hp; /* another temp */
p = &piece[pidx];
wasonboard = onboard(p);
if (wasonboard) {
(*gridp)[p->grid.h][p->grid.v] = (struct pieceimage *) 0;
}
p->grid.h = NOGRID; p->grid.v = NOGRID;
if (wasonboard || !onscreen(p)) {
switch(classbase(p)) {
case FIRSTSWEDE:
hp = &swedhome[0]; lasthp = &swedhome[LASTSWEDE - FIRSTSWEDE];
break;
case FIRSTMUSC:
hp = &muschome[0]; lasthp = &muschome[LASTMUSC - FIRSTMUSC];
break;
case THEKING:
hp = &kinghome[0]; lasthp = &kinghome[0];
break;
}
while (hp <= lasthp && hp->tenant) {
++hp;
}
if (hp > lasthp) {
hp = &weirdhome; /* should never happen */
}
hp->tenant = p;
p->curhome = hp;
}
sweeppiece(pidx, &(p->curhome->hpoint));
}
/*
* hidepiece() - move a piece off the screen
*/
hidepiece(pidx)
int pidx; /* index of the piece to hide */
{
struct pieceimage *p;
p = &piece[pidx];
if (onscreen(p) && !onboard(p)) {
p->curhome->tenant = (struct pieceimage *) 0;
p->curhome = &weirdhome;
}
if (onboard(p)) {
(*gridp)[p->grid.h][p->grid.v] = (struct pieceimage *) 0;
}
p->grid.h = NOGRID; p->grid.v = NOGRID;
drawpiece(pidx, NOPLACE, NOPLACE);
}